package de.breitbeil.samsyncro.lib.util;

import java.io.BufferedReader;
import java.io.File;
import java.io.FileReader;
import java.io.IOException;
import java.io.InputStream;
import java.util.ArrayList;
import java.util.List;
import java.util.Properties;

import de.breitbeil.samsyncro.lib.Application;
import de.breitbeil.samsyncro.lib.domain.pim.SVCard;

public class Util {

    public static String toHexString(String s) {
        StringBuilder builder = new StringBuilder();
        
        byte[] b = s.getBytes();
        for(int i = 0; i < b.length; i++) {
            int hex = b[i];
            builder.append(Integer.toHexString(hex) +" ");
        }
        
        return builder.toString();
    }
    
    public static String toHexString(byte[] s) {
        StringBuilder builder = new StringBuilder();
        
        for(int i = 0; i < s.length; i++) {
            int hex = s[i];
            builder.append(Integer.toHexString(hex) +" ");
        }
        
        return builder.toString();
    }
    
    public static String hexToString(byte[] s) {
        String text = new String(s);
        return text;
    }
    
    public static int[] signedToUnsigned(byte[] s) {
        int[] result = new int[s.length];
        
        for(int i = 0; i < s.length; i++) {
            result[i] = (0x000000FF & (int)s[i] );
        }
        
        return result;
    }

    public static int unsingnedWordToInt(byte[] s) {
        if(s.length != 2) {
            throw new IllegalArgumentException("Byte array must be of size 2.");
        }
        return unsingnedWordToInt(s, 0);
    }
    
    public static int unsingnedWordToInt(byte[] s, int offset) {
        int[] i = signedToUnsigned(s);
        
        return i[offset] * 256 + i[offset+1];
    }
    
    public static List<SVCard> getvCards(File file) throws IOException {
        final String delimiter = "END:VCARD\r\n";
        final List<SVCard> list = new ArrayList<SVCard>();
        String cards = readTextFile(file);
        String[] vcards = cards.split(delimiter);
        
        for(int i = 0; i < vcards.length; i++) {
            vcards[i] = vcards[i].trim();
            String[] vcard = vcards[i].split("\r\n", 2);
            
            if(vcard.length == 2) {
                SVCard svcard = new SVCard();
                svcard.setId(Integer.parseInt( vcard[0].substring(3) ));
                svcard.setVcard(vcard[1] + "END:VCARD");
                
                list.add(svcard);
            }
        }
        
        return list;
    }
    
    public static String readTextFile(File file) throws IOException {
        BufferedReader in = new BufferedReader(new FileReader(file));
        StringBuilder builder = new StringBuilder();
        String str;
        
        while ((str = in.readLine()) != null) {
            builder.append(str+"\r\n");
        }
        
        in.close();

        return builder.toString();
    }

    public static String getBuild() {
        InputStream is = Application.class.getResourceAsStream("settings.properties");
        Properties prop = new Properties();
        
        try {
            prop.load(is);
            return (String) prop.get("build");
        } catch (IOException e) {
            return "No version found!"+e.getMessage();
        }
    }
    
    public static String getVersion() {
        InputStream is = Application.class.getResourceAsStream("settings.properties");
        Properties prop = new Properties();
        
        try {
            prop.load(is);
            return (String) prop.get("version");
        } catch (IOException e) {
            return "No version found!"+e.getMessage();
        }
    }
    
}
